﻿using System;
using UnityEngine;
using Utilities;

namespace GlobalManager
{
    
    public class RoundTurnManager : Singleton<RoundTurnManager>
    {
        public bool isBattling = false;
        public bool isPlayerTurn = false;
        public bool isEnemyTurn = false;
        public TurnPhase currentPhase = TurnPhase.None;

        public bool playerDefeated = false;
        public bool enemyDefeated = false;
        
        public bool IsPlayerTurn => isPlayerTurn;
        public bool IsEnemyTurn => isEnemyTurn;
        public TurnPhase CurrentPhase => currentPhase;


        private void OnEnable()
        {
            OnEventHandler.PlayerDrawEnd += OnPlayerDrawEnd;
            OnEventHandler.PlayerUseCardEnd += OnPlayerUseCardEnd;
            OnEventHandler.PlayerActionEnd += OnPlayerActionEnd;
            OnEventHandler.PlayerTurnEnd += EndPlayerTurn;

            OnEventHandler.EnemyDrawEnd += OnEnemyDrawEnd;
            OnEventHandler.EnemyUseCardEnd += OnEnemyUseCardEnd;
            OnEventHandler.EnemyActionEnd += OnEnemyActionEnd;
            OnEventHandler.EnemyTurnEnd += EndEnemyTurn;
        }

        private void OnDisable()
        {
            OnEventHandler.PlayerDrawEnd -= OnPlayerDrawEnd;
            OnEventHandler.PlayerUseCardEnd -= OnPlayerUseCardEnd;
            OnEventHandler.PlayerActionEnd -= OnPlayerActionEnd;
            OnEventHandler.PlayerTurnEnd -= EndPlayerTurn;

            OnEventHandler.EnemyDrawEnd -= OnEnemyDrawEnd;
            OnEventHandler.EnemyUseCardEnd -= OnEnemyUseCardEnd;
            OnEventHandler.EnemyActionEnd -= OnEnemyActionEnd;
            OnEventHandler.EnemyTurnEnd -= EndEnemyTurn;
        }

        // ！！！！！ 遇战 ！！！！！
        public void BattleBegin()
        {
            isBattling = true;
            Debug.Log("战斗开始！");
            StartPlayerTurn();
        }

        #region ------------------------ 玩家回合 ------------------------

        public void StartPlayerTurn()
        {
            if (!isBattling) return;

            isPlayerTurn = true;
            isEnemyTurn = false;
            Debug.Log("玩家回合开始");
            OnEventHandler.CallPlayerTurnBegin();

            StartPlayerDrawPhase();
        }

        private void StartPlayerDrawPhase()
        {
            currentPhase = TurnPhase.Draw;
            Debug.Log("玩家抽卡阶段");
            OnEventHandler.CallPlayerDrawBegin();
        }

        private void OnPlayerDrawEnd()
        {
            StartPlayerUseCardPhase();
        }

        private void StartPlayerUseCardPhase()
        {
            currentPhase = TurnPhase.UseCard;
            Debug.Log("玩家出牌阶段");
            OnEventHandler.CallPlayerUseCardBegin();
        }

        private void OnPlayerUseCardEnd()
        {
            StartPlayerActionPhase();
        }

        private void StartPlayerActionPhase()
        {
            currentPhase = TurnPhase.Action;
            Debug.Log("玩家行动阶段");
            OnEventHandler.CallPlayerActionBegin();
        }

        private void OnPlayerActionEnd()
        {
            EndPlayerTurn();
        }

        private void EndPlayerTurn()
        {
            if (!isBattling || !isPlayerTurn) return;

            isPlayerTurn = false;
            Debug.Log("玩家回合结束，进入敌我回合过渡阶段");
            OnEventHandler.CallPlayerTurnEnd();

            TransitionToEnemyTurn();
        }

        private void TransitionToEnemyTurn()
        {
            Debug.Log("进入敌人回合");
            StartEnemyTurn();
        }

        #endregion

        #region ------------------------ 敌人回合 ------------------------

        public void StartEnemyTurn()
        {
            if (!isBattling) return;

            isEnemyTurn = true;
            Debug.Log("敌人回合开始");
            OnEventHandler.CallEnemyTurnBegin();

            StartEnemyDrawPhase();
        }

        private void StartEnemyDrawPhase()
        {
            currentPhase = TurnPhase.Draw;
            Debug.Log("敌人抽卡阶段");
            OnEventHandler.CallEnemyDrawBegin();
        }

        private void OnEnemyDrawEnd()
        {
            StartEnemyUseCardPhase();
        }

        private void StartEnemyUseCardPhase()
        {
            currentPhase = TurnPhase.UseCard;
            Debug.Log("敌人出牌阶段");
            OnEventHandler.CallEnemyUseCardBegin();
        }

        private void OnEnemyUseCardEnd()
        {
            StartEnemyActionPhase();
        }

        private void StartEnemyActionPhase()
        {
            currentPhase = TurnPhase.Action;
            Debug.Log("敌人行动阶段");
            OnEventHandler.CallEnemyActionBegin();
        }

        private void OnEnemyActionEnd()
        {
            EndEnemyTurn();
        }

        private void EndEnemyTurn()
        {
            if (!isBattling || !isEnemyTurn) return;

            isEnemyTurn = false;
            Debug.Log("敌人回合结束，进入回合过渡阶段");
            OnEventHandler.CallEnemyTurnEnd();

            TransitionToNextRound();
        }

        private void TransitionToNextRound()
        {
            Debug.Log("进入新轮次");
            StartNewRound();
        }

        #endregion

        // ------------------------ 轮次控制 ------------------------
        private void StartNewRound()
        {
            Debug.Log("新轮次开始");

            if (playerDefeated)
            {
                isBattling = false;
                Debug.Log("玩家战败，游戏结束");
                OnEventHandler.CallPlayerDefeated();
                return;
            }

            if (enemyDefeated)
            {
                isBattling = false;
                Debug.Log("敌人战败，玩家胜利");
                OnEventHandler.CallEnemyDefeated();
                return;
            }

            StartPlayerTurn();
        }

        //TODO  ------------------------ 胜负判断(执行条件尚不明晰) ------------------------
        
        public void PlayerDefeated()
        {
            playerDefeated = true;
        }

        public void EnemyDefeated()
        {
            enemyDefeated = true;
        }
    }
}
